package com.wuhunyu.task;

import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.service.StatisticsDailyService;
import com.wuhunyu.servicebase.exception.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Calendar;

/**
 * 统计用户信息
 *
 * @author wuhunyu
 * @version 1.0
 * @date 2021-01-27 19:34
 */
@Component
@Slf4j
public class CountTask {

    @Autowired
    private StatisticsDailyService statisticsDailyService;

    /**
     * 每天凌晨1点执行
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void countUser() {
        // 获取前一天的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        CommonResult commonResult = statisticsDailyService.countUser(calendar.getTime());
        log.info("当前时间：{}，统计时间：{}", Calendar.DATE, calendar.getTime());
        Assert.isRight(commonResult == null || commonResult.getData() == null || (int) commonResult.getData().get("code") != 200, "统计用户信息失败");
    }

}
